<?php

namespace plugin\ycMassage\process;

use Exception;
use plugin\ycMassage\app\model\MassageMchBill;
use plugin\ycMassage\app\model\MassageOrders;
use plugin\ycMassage\app\model\MassageUserAgentBill;
use plugin\ycMassage\app\model\MassageUserChannelsBill;
use plugin\ycMassage\app\model\MassageUserDealerBill;
use plugin\ycMassage\app\model\MassageUserFreezeMoney;
use plugin\ycMassage\app\model\MassageUserSalesmanBill;
use plugin\ycMassage\app\model\MassageUserTechnicianBill;
use plugin\ycMassage\utils\enum\ActionIdentity;
use plugin\ycMassage\utils\enum\State;
use think\facade\Db;

class UserUnfreezingMoney
{
    public function run()
    {
        try {
            $MassageUserFreezeMoney = MassageUserFreezeMoney::where(['state' => State::NO['value']])
                ->whereTime('unfreezing_time', '<', date('Y-m-d H:i:s'))->select();
            foreach ($MassageUserFreezeMoney as $item) {
                Db::startTrans();
                try {
                    $MassageOrders = MassageOrders::where(['id' => $item->orders_id])->find();
                    switch ($item->action) {
                        case ActionIdentity::TECHNICIAN['value']:
                            MassageUserTechnicianBill::increase($item->remarks ?? '完成订单，订单号：' . $MassageOrders->trade, $item->uid, $item->money, $item->form_uid, true);
                            break;
                        case ActionIdentity::AGENT['value']:
                            MassageUserAgentBill::increase($item->remarks ?? '订单号：' . $MassageOrders->trade, $item->uid, $item->money, $item->form_uid, true);
                            break;
                        case ActionIdentity::MCH['value']:
                            MassageMchBill::increase($item->remarks ?? '订单号：' . $MassageOrders->trade, $item->uid, $item->money, $item->form_uid, true);
                            break;
                        case ActionIdentity::CHANNELS['value']:
                            MassageUserChannelsBill::increase($item->remarks ?? '订单号：' . $MassageOrders->trade, $item->uid, $item->money, $item->form_uid, true);
                            break;
                        case ActionIdentity::DEALER['value']:
                            MassageUserDealerBill::increase($item->remarks ?? '订单号：' . $MassageOrders->trade, $item->uid, $item->money, $item->form_uid, true);
                            break;
                        case ActionIdentity::SALESMAN['value']:
                            MassageUserSalesmanBill::increase($item->remarks ?? '订单号：' . $MassageOrders->trade, $item->uid, $item->money, $item->form_uid, true);
                            break;
                    }
                    $item->state = State::YES['value'];
                    $item->save();
                    Db::commit();
                } catch (\Throwable $th) {
                    Db::rollback();
                    p([$th->getMessage(), $th->getLine(), $th->getFile()], 'MassageUserUnfreezingMoney');
                }
            }
        } catch (\Throwable $th) {
            p([$th->getMessage(), $th->getLine(), $th->getFile()],'MassageUserUnfreezingMoney');
        }
    }
}
